﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class UserWork_Check : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        this.MySpan.Visible = false;
        this.MySpan1.Visible = false;
        this.Button1.Attributes.Add("onclick", "javascript:return confirm('确认考勤?')");
        this.Button4.Attributes.Add("onclick", "javascript:return confirm('确认考勤?')");

       
        if (!IsPostBack)
        {

           
            Bind();
        }
    }
    protected void btnCheckAttendance_Click(object sender, EventArgs e)
    {
        string UserName = Session["UserName"].ToString();
        int i=1;
        SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string Da = DateTime.Now.ToLongDateString();
        string sql = "select * from [Check] where UserName='"+UserName+ "' and shang="+i+" order by OndutyId desc";
        SqlCommand com = new SqlCommand(sql,con);
        SqlDataReader dr = com.ExecuteReader();
       
        if (dr.Read())
        {
            string Check = dr["Checkdate"].ToString();
            if (Check == Da)
            {
                Response.Write("<script>alert('你已完成上班考勤！')</script>");

                this.MySpan1.Visible = false;
            }
            else
            {
                this.MySpan1.Visible = false;
                this.MySpan.Visible = true;
            }
           
        }
        else
        {
            this.MySpan1.Visible = false;
            this.MySpan.Visible = true;
        }
       
        con.Close();

    }
    
    public void Bind()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from  [Check] where userName='"+Session["UserName"].ToString()+"'";
        SqlDataAdapter da = new SqlDataAdapter(sql,con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void btnCheckAttendance_Disposed(object sender, EventArgs e)
    {
       
     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from Checktime ";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        DateTime shang = Convert.ToDateTime(dr["ShangTime"].ToString());
        DateTime time = Convert.ToDateTime(DateTime.Now.ToString());
        TimeSpan d = shang.TimeOfDay;
        TimeSpan dd = time.TimeOfDay;
        con.Close();
        if (d > dd)
        {
            int i = 1;
            int b = 0;
            string Da = DateTime.Now.ToLongDateString();
            string state = "考勤成功";
            string id = Session["UserName"].ToString();
            string Content = this.TextBox1.Text;
            SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con1.Open();
            Response.Write("<script>alert('考勤成功！')</script>");
            string sql1 = "insert into  [Check] values('" + time.ToString() + "',null,'" + Da + "','" + id + "','" + state + "',null,'"+Content+"',null,"+i+","+b+")";
            SqlCommand com1 = new SqlCommand(sql1, con1);
            com1.ExecuteNonQuery();
            Bind();
            con1.Close();
            
        }
        else
        {
            int i = 1;
            int b = 0;
            string Da = DateTime.Now.ToLongDateString();
            string id = Session["UserName"].ToString();
            string state = "迟到";
            string Content = this.TextBox1.Text;
            SqlConnection con2 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con2.Open();
            DateTime Time1 = DateTime.Now;
            string sql2 = "insert into [Check] values('" + Time1.ToString() + "',null,'" + Da + "','" + id + "','" + state + "',null,'" + Content + "',null,"+i+","+b+")";
            SqlCommand com2 = new SqlCommand(sql2, con2);
            com2.ExecuteNonQuery();
            con2.Close();
            Bind();
            //btnCheckAttendance.Attributes.Add("onclick", "javascript:return confirm('上班迟到,需要填写理由吗?')"))
            //Response.Write("<script>\return confirm('')</script>");


        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.MySpan.Visible = false;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        int i = 1;
        SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con1.Open();
        string Da1 = DateTime.Now.ToLongDateString();
        string sql1 = "select * from [Check] where UserName='" + Session["UserName"].ToString() + "' and shang=" + i + " order by OndutyId desc";
        SqlCommand com1 = new SqlCommand(sql1, con1);
        SqlDataReader dr1 = com1.ExecuteReader();
        if (dr1.Read())
        {
            string Check1= dr1["Checkdate"].ToString();
            if (Check1 == Da1)
            {

                int c = 1;
                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string Da = DateTime.Now.ToLongDateString();
                string sql = "select * from [Check] where UserName='" + Session["UserName"].ToString() + "' and xia=" + c + " order by OndutyId desc";
                SqlCommand com = new SqlCommand(sql, con);
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    string Check = dr["Checkdate"].ToString();
                    if (Check == Da)
                    {
                        Response.Write("<script>alert('你已完成下班考勤！')</script>");
                        this.MySpan.Visible = false;

                    }
                    else
                    {
                        this.MySpan.Visible = false;
                        this.MySpan1.Visible = true;
                    }

                }
                else
                {
                    this.MySpan.Visible = false;
                    this.MySpan1.Visible = true;
                }

                con.Close();
            }
            else
            {
                Response.Write("<script>alert('对不起,你上班还未考勤！')</script>");
            }

        }
        else
        {
            Response.Write("<script>alert('对不起,你上班还未考勤！')</script>");
        }

        con1.Close();



    }

    protected void Button4_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        string sql = "select * from Checktime ";
        SqlCommand com = new SqlCommand(sql, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        DateTime xia = Convert.ToDateTime(dr["xiaTime"].ToString());
        DateTime time = Convert.ToDateTime(DateTime.Now.ToString());
        TimeSpan d = xia.TimeOfDay;
        TimeSpan dd = time.TimeOfDay;
        con.Close();
        if (d>dd)
        {
            DateTime dt = DateTime.Now;
            string Da = DateTime.Now.ToLongDateString();
            string y="早退";
            string Whys = this.TextBox2.Text.Trim();
            int s = 1;
            int x = 1;
            SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con1.Open();
            string sql1 = "update [Check] set offdutyTime='"+dt.ToString()+"',offdutystate='"+y+"',offwhys='"+Whys+"',shang="+s+",xia="+x+" where username='"+Session["UserName"].ToString()+"' and CheckDate='"+Da+"'";
            SqlCommand com1 = new SqlCommand(sql1,con1);
            com1.ExecuteNonQuery();
            con1.Close();
            Bind();
            
        }
        else
        {
            DateTime dt = DateTime.Now;
            string Da = DateTime.Now.ToLongDateString();
            string y = "成功考勤";
            string Whys = this.TextBox2.Text.Trim();
            int s = 1;
            int x = 1;
            SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
            con1.Open();
            string sql1 = "update [Check] set offdutyTime='" + dt.ToString() + "',offdutystate='" + y + "',offwhys='" + Whys + "',shang=" + s + ",xia=" + x + " where username='" + Session["UserName"].ToString() + "' and CheckDate='" + Da + "'";
            SqlCommand com1 = new SqlCommand(sql1, con1);
            com1.ExecuteNonQuery();
            con1.Close();
            Bind();
        }
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        this.MySpan.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button6_Click(object sender, EventArgs e)
    {

    }
}
